﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using Newtonsoft.Json;

namespace OF.JsonRpc.Gateway
{
    public abstract class BaseAuthGateway : IGateway
    {
        public virtual void PreProcess(string jsonRpc)
        {
            JsonRequest firstJsonRequest;

            if (isSingleRpc(jsonRpc))
            {
                firstJsonRequest = JsonConvert.DeserializeObject<JsonRequest>(jsonRpc);
            }
            else
            {
                firstJsonRequest = JsonConvert.DeserializeObject<JsonRequest[]>(jsonRpc)[0];
            }
            VerifyAuth(firstJsonRequest);
        }

        protected abstract void VerifyAuth(JsonRequest jsonRequest);

        public virtual void CompletedPocess(string jsonRpcResponse)
        {
            
        }

        public Dictionary<string, string> Attributes { get; set; }

        protected static bool isSingleRpc(string json)
        {
            for (int i = 0; i < json.Length; i++)
            {
                if (json[i] == '{') return true;
                else if (json[i] == '[') return false;
            }
            return true;
        }
    }
}
